package cn.terys.terystransactionclient;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.terys.terystransactionclient.data.NetManager;

public class BackQueryActivity extends BaseActivity
{

    public static final String INTENT_EXTRANAME_QUERYRESULT = "QUERYRESULT";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.backquery);
        setTitle(R.string.backquery_title);

        final EditText orderNum = (EditText) findViewById(R.id.order_num);
        Button query = (Button) findViewById(R.id.query);
        query.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String orderStr = orderNum.getText().toString();
                if (TextUtils.isEmpty(orderStr))
                {
                    ToastManager.showLong(BackQueryActivity.this, R.string.backquery_query_empty);
                    return;
                }
                new GenericAsyncTask<String, Object, String[]>(BackQueryActivity.this)
                {
                    @Override
                    protected String[] doInBackgroundImpl(String... arg0) throws Exception
                    {
                        // TODO Auto-generated method stub
                        return NetManager.backQuery(MainActivity.BATCH_NUM, arg0[0]);
                    }

                    protected void onPostExecute(Object[] objs, String[] result)
                    {
                        // TODO Auto-generated method stub
                        super.onPostExecute(objs, result);
                        Context context = (Context) objs[0];
                        Intent intent = new Intent(context, BackQueryResultActivity.class);
                        intent.putExtra(INTENT_EXTRANAME_QUERYRESULT, result);
                        context.startActivity(intent);
                    }
                }.execute(orderStr);
            }
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

    }

}